﻿using Little.Soldier.SWIFT.Validation;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml;

namespace Little.Soldier.SWIFT
{
  [Serializable]
  public abstract class SwiftBlockBase : IValidator
  {
    private UnparsedTextList _UnparsedTexts = new UnparsedTextList();
    private List<IValidate> _Rules = new List<IValidate>();
    private long _Id;
    private SwiftMessage _SwiftMessage;

    public virtual List<IValidate> Rules
    {
      get
      {
        return this._Rules;
      }
    }

    public SwiftMessage SwiftMessage
    {
      get
      {
        return this._SwiftMessage;
      }
      set
      {
        this._SwiftMessage = value;
      }
    }

    public virtual long Id
    {
      get
      {
        return this._Id;
      }
      set
      {
        this._Id = value;
      }
    }

    public abstract string BlockName { get; }

    public abstract int BlockNumber { get; }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Browsable(true)]
    internal virtual UnparsedTextList UnparsedTexts
    {
      get
      {
        return this._UnparsedTexts;
      }
      set
      {
        this._UnparsedTexts = value;
      }
    }

    internal virtual int UnparsedTextsSize
    {
      get
      {
        if (this.UnparsedTexts == null)
          return 0;
        else
          return this.UnparsedTexts.Size;
      }
    }

    public virtual bool IsTagBlock
    {
      get
      {
        return this is SwiftTagListBlock;
      }
    }

    public SwiftBlockBase(SwiftMessage swiftMessage)
    {
      this.SwiftMessage = swiftMessage;
      this.InitializeRules();
    }

    internal SwiftBlockBase(SwiftMessage swiftMessage, UnparsedTextList unparsedText)
      : this(swiftMessage)
    {
      this.UnparsedTexts = unparsedText;
    }

    public abstract void InitializeRules();

    public virtual bool Validate()
    {
      return this.Validate(new List<ValidationError>());
    }

    public virtual bool Validate(List<ValidationError> errorList)
    {
      if (errorList == null)
        errorList = new List<ValidationError>();
      int count = errorList.Count;
      foreach (IValidate validate in this.Rules)
        validate.Validate(errorList);
      return count >= errorList.Count;
    }

    public override bool Equals(object obj)
    {
      if (this != obj)
        goto label_8;
label_7:
      return true;
label_8:
      if (obj == null || this.GetType() != obj.GetType())
        return false;
      SwiftBlockBase swiftBlockBase = (SwiftBlockBase) obj;
      if (this.UnparsedTexts == null)
      {
        if (swiftBlockBase.UnparsedTexts != null)
          return false;
        else
          goto label_7;
      }
      else if (!this.UnparsedTexts.Equals((object) swiftBlockBase.UnparsedTexts))
        return false;
      else
        goto label_7;
    }

    public override int GetHashCode()
    {
      return 31 * 1 + (this.UnparsedTexts != null ? this.UnparsedTexts.GetHashCode() : 0);
    }

        internal virtual void AddTex(SwiftMessage message)
        {
            this.UnparsedTexts.AddTex(message);
        }

        internal virtual void AddTex(string text)
        {
            this.UnparsedTexts.AddTex(text);
        }

    internal virtual SwiftMessage U0086(int index)
    {
      return this.UnparsedTexts.U0083U0003(index);
    }

    internal virtual string U0087(int index)
    {
      return this.UnparsedTexts.U0082U0003(index);
    }

    internal virtual bool U0088(int index)
    {
      return this.UnparsedTexts.U0084U0003(index);
    }

    internal virtual XmlNode ToXml(XmlDocument doc)
    {
      throw new NotImplementedException("Block.ToXml()");
    }
  }
}
